package cn.imokay.stopthread;

/**
 * run方法有sleep
 */
public class RightWayStopThreadWithSleep {


    public static void main(String[] args) throws InterruptedException {

        Runnable r = () -> {
            int num = 0;
            try {
                while (!Thread.currentThread().isInterrupted() && num <= 300) {
                    if (num % 100 == 0) {
                        System.out.println(num + "是100的整数倍");
                    }
                    num++;
                }

                Thread.sleep(1000);
            } catch (InterruptedException e) {
                //sleep的过程中，如果收到终止通知，则会抛出异常用以中断线程，捕获异常后需要处理收到中断通知后的一些收尾工作
                throw new RuntimeException(e);
            }
            System.out.println("finished");
        };
        Thread thread = new Thread(r);
        thread.start();
        Thread.sleep(500);
        thread.interrupt();
    }
}
